<template>
  <div class="container">
    <div class="top">
      <el-input
        style="width: 150px; margin-bottom: 5px"
        v-model="search.out_code"
        placeholder="请输入出库单号"
      ></el-input>
      <el-input
        style="width: 150px; margin-bottom: 5px"
        v-model="search.inp_code"
        placeholder="请输入出库单号"
      ></el-input>

      入库时间：
      <el-date-picker
        value-format="yyyy-MM-dd"
        v-model="date"
        type="daterange"
        :clearable="false"
        range-separator="至"
        f
        start-placeholder="入库开始时间"
        end-placeholder="入库结束时间"
      ></el-date-picker>
      出库时间：
      <el-date-picker
        value-format="yyyy-MM-dd"
        v-model="date1"
        type="daterange"
        :clearable="false"
        range-separator="至"
        start-placeholder="出库开始时间"
        end-placeholder="出库结束时间"
      ></el-date-picker>
      <el-input
        style="width: 300px"
        :disabled="true"
        placeholder="请搜索商品信息"
        v-model="commodityNameList.name"
      >
        <template slot="prepend"
          >商品</template
        >
        <el-button
          slot="append"
          icon="el-icon-search"
          @click="commodityBtn"
        ></el-button>
      </el-input>
      <el-input
        style="width: 300px"
        :disabled="true"
        placeholder="请搜索类别信息"
        v-model="classNameList.name"
      >
        <template slot="prepend"
          >类别</template
        >
        <el-button
          slot="append"
          icon="el-icon-search"
          @click="classBtn"
        ></el-button>
      </el-input>
      周转天数:
      <el-input
        style="width: 150px; margin-bottom: 5px"
        v-model="search.min_turnover_days"
        placeholder="请输入周转天数"
      ></el-input>
      至
      <el-input
        style="width: 150px; margin-bottom: 5px"
        v-model="search.max_turnover_days"
        placeholder="请输入周转天数"
      ></el-input>
      <el-button
        size="small"
        type="primary"
        icon="el-icon-search"
        class="handleSearch"
        @click="handleSearch()"
        >查询</el-button
      >
      <el-button
        size="small"
        type="danger"
        class="handleSearch"
        icon="el-icon-delete"
        @click="empty()"
        >清空</el-button
      >
      <el-button
        type="info"
        size="small"
        icon="el-icon-tickets"
        @click="derive()"
        >导出</el-button
      >
    </div>
    <el-table
      max-height="600"
      :data="tableData"
      border
      style="width: 100%"
      empty-text="暂无数据"
    >
      <el-table-column
        prop="out_code"
        label="出库批次编码"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="inp_code"
        label="入库批次编码"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="erp_goods_code"
        label="erp商品编码"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="goods_name"
        label="商品名称"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="goods_bar_code"
        label="条码"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="inp_time"
        label="入库时间"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="out_time"
        label="出库时间"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="turnover_days"
        label="周转天数"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="category_code"
        label="商品类别"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="category_name"
        label="商品类别名称"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="num"
        label="周转数量"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="qty"
        label="周转件数"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="price"
        label="单价"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="amount"
        label="总金额"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="rk_spec"
        label="入库规格"
        align="center"
      ></el-table-column>
    </el-table>
    <!-- 分页 -->
    <pagination
      @page="handleSizeChange"
      @pagesize="handleCurrentChange"
      :pageData="pageData"
    ></pagination>
    <commodityDialog
      :commoditydialog="commoditydialog"
      @tag="commodityTag"
    ></commodityDialog>
    <classDialog :classdialog="classdialog" @tag="classTag"></classDialog>
  </div>
</template>
<script>
import { getnowDate } from "@/utils/index";
import Pagination from "@/components/pagination";
import ClassDialog from "@/components/superGrossMargin/classDialog";
import CommodityDialog from "@/components/superGrossMargin/commodityDialog";
import { turnoverList } from "@/api/commodity/commodityList";
import { getToken } from "@/utils/auth";

export default {
  components: {
    pagination: Pagination,
    commodityDialog: CommodityDialog,
    classDialog: ClassDialog
  },
  data() {
    return {
      //页面数据
      tableData: [],
      //分页
      pageData: {
        pageSizes: [10, 20],
        pageSize: 10,
        page: 1,
        total: null,
        layout: "total, sizes, prev, pager, next, jumper"
      },
      //默认时间
      normal_startime: getnowDate().startimer,
      normal_endtime: getnowDate().endtimer,
      search: {
        out_code: "",
        inp_code: "",
        classid: "",
        goods_id: "",
        inp_start_time: "",
        inp_end_time: "",
        out_start_time: "",
        out_end_time: "",
        min_turnover_days: "",
        max_turnover_days: ""
      },
      date: [],
      date1: [],
      //dialog信息
      commoditydialog: {
        centerDialogVisible: false
      },
      classdialog: {
        centerDialogVisible: false
      },

      commodityNameList: {
        name: "",
        code: ""
      },
      classNameList: {
        name: "",
        code: ""
      },
      warehouseList: {
        name: "",
        code: ""
      }
    };
  },
  created() {
    this.search.out_start_time = this.normal_startime;
    this.search.out_end_time = this.normal_endtime;
    if (this.normal_startime) {
      this.date1.push(this.search.out_start_time);
    }
    if (this.normal_endtime) {
      this.date1.push(this.search.out_end_time);
    }
    this.getData();
  },
  methods: {
    //获取页面数据
    getData() {
      let data = {
        limit: this.pageData.pageSize,
        page: this.pageData.page,
        out_code: this.search.out_code,
        inp_code: this.search.inp_code,
        category_code: this.classNameList.code,
        goods_code: this.commodityNameList.code,
        inp_start_time: this.search.inp_start_time,
        inp_end_time: this.search.inp_end_time,
        out_start_time: this.search.out_start_time,
        out_end_time: this.search.out_end_time,
        min_turnover_days: this.search.min_turnover_days,
        max_turnover_days: this.search.max_turnover_days
      };

      turnoverList(data)
        .then(res => {
          if (res.code == 0) {
            this.tableData = res.data.list.data;
            this.pageData.page = res.data.list.current_page;
            this.pageData.pagesize = res.data.list.per_page;
            this.pageData.total = res.data.list.total;
          }
        })
        .catch(err => {
          console.log(err);
        });
    },
    commodityTag(data) {
      this.commodityNameList = {
        name: "",
        code: ""
      };
      console.log(data, 11111);
      let arrName = [];
      let arrCode = [];
      data.forEach(res => {
        arrName.push(res.name);
        arrCode.push(res.code);
      });

      this.commodityNameList.name = arrName.join(",");
      this.commodityNameList.code = arrCode.join(",");
    },
    classTag(data) {
      this.classNameList = {
        name: "",
        code: ""
      };
      console.log(data, 11111);
      let arrName = [];
      let arrCode = [];
      data.forEach(res => {
        arrName.push(res.name);
        arrCode.push(res.code);
      });
      this.classNameList.name = arrName.join(",");
      this.classNameList.code = arrCode.join(",");
    },

    //超级商品搜索
    commodityBtn() {
      this.commoditydialog = {
        centerDialogVisible: true
      };
    },
    //超级类别搜索
    classBtn() {
      this.classdialog = {
        centerDialogVisible: true
      };
    },

    // 本地搜索
    handleSearch() {
      if (this.date.length > 0) {
        this.search.inp_start_time = this.date[0];
        this.search.inp_end_time = this.date[1];
      }
      if (this.date1.length > 0) {
        this.search.out_start_time = this.date1[0];
        this.search.out_end_time = this.date1[1];
      }
      this.pageData.page = 1;
      this.pageData.total = null;
      this.getData();
    },
    //导出
    derive() {
      this.$confirm("是否导出?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          if (this.date.length > 0) {
            this.search.inp_start_time = this.date[0];
            this.search.inp_end_time = this.date[1];
          }
          if (this.date1.length > 0) {
            this.search.out_start_time = this.date1[0];
            this.search.out_end_time = this.date1[1];
          }
          let link = document.createElement("a");
          link.href =
            this.$axios.defaults.baseURL +
            "/smkapi/Goods/exportTurnoverData?token=" +
            getToken() +
            "&out_code=" +
            this.search.out_code +
            "&inp_code=" +
            this.search.inp_code +
            "&category_code=" +
            this.classNameList.code +
            "&goods_code=" +
            this.commodityNameList.code +
            "&inp_start_time=" +
            this.search.inp_start_time +
            "&inp_end_time=" +
            this.search.inp_end_time +
            "&min_turnover_days=" +
            this.search.min_turnover_days +
            "&max_turnover_days=" +
            this.search.max_turnover_days +
            "&out_start_time=" +
            this.search.out_start_time +
            "&out_end_time=" +
            this.search.out_end_time;
          link.click();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消导出"
          });
        });
    },
    //清空
    empty() {
      this.date = [];
      this.date1 = [];
      this.date1.push(this.normal_startime);
      this.date1.push(this.normal_endtime);
      this.search = {
        out_code: "",
        inp_code: "",
        classid: "",
        goods_id: "",
        inp_start_time: "",
        inp_end_time: "",
        min_turnover_days: "",
        max_turnover_days: "",
        out_start_time: this.normal_startime,
        out_end_time: this.normal_endtime
      };
      this.classNameList.name = "";
      this.commodityNameList.name = "";
      this.classNameList.code = "";
      this.commodityNameList.code = "";
    },
    //每页多少条
    handleCurrentChange(val) {
      this.pageData.page = 1;
      this.pageData.pageSize = val;
      this.getData();
    },
    //选取页数
    handleSizeChange(page) {
      this.pageData.page = page;
      this.getData();
    }
  }
};
</script>
<style scoped>
.top {
  position: relative;
  margin-bottom: 5px;
}

.add {
  position: absolute;
  right: 0;
  top: 4px;
}
.pagination {
  text-align: right;
  padding: 10px;
}
</style>
